package demo.db.maxmind;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Subdivision;
import com.maxmind.geoip2.record.Traits;
import lombok.Data;

import java.util.List;

/**
 * <pre>
 *     {
 *     "ip": "183.222.21.252",
 *     "countryName": "China",
 *     "countryZhName": "中国",
 *     "countryCode": "CN",
 *     "subdivisionZhName": "四川",
 *     "subdivisionName": "Sichuan",
 *     "subdivisionCode": "SC",
 *     "cityName": "Chengdu",
 *     "cityZhName": "成都",
 *     "longitude": "104.0667",
 *     "latitude": "30.6667"
 *   }
 * </pre>
 */
@Data
public class CommonRep {
    private String cityZhName;
    private String cityName;
    private String countryCode;
    private String ip;
    private String latitude;
    private String countryName;
    private String subdivisionCode;
    private String countryZhName;
    private String subdivisionName;
    private String subdivisionZhName;
    private String longitude;

    /**
     * 转换
     *
     * @param cityRep
     * @return
     */
    public static CommonRep from(CityResponse cityRep) {
        if (cityRep == null) {
            return null;
        }
        CommonRep vo = new CommonRep();
        Traits traits = cityRep.getTraits();
        if (traits != null) {
            vo.setIp(traits.getIpAddress());
        }
        Country country = cityRep.getCountry();
        if (country != null) {
            vo.setCountryCode(country.getIsoCode());
            vo.setCountryName(country.getNames().get("en"));
            vo.setCountryZhName(country.getNames().get("zh-CN"));
        }
        List<Subdivision> subdivisions = cityRep.getSubdivisions();
        if (subdivisions != null && !subdivisions.isEmpty()) {
            Subdivision subdivision = subdivisions.get(0);
            vo.setSubdivisionCode(subdivision.getIsoCode());
            vo.setSubdivisionName(subdivision.getNames().get("en"));
            vo.setSubdivisionZhName(subdivision.getNames().get("zh-CN"));
        }
        City city = cityRep.getCity();
        if (city != null) {
            vo.setCityName(city.getNames().get("en"));
            vo.setCityZhName(city.getNames().get("zh-CN"));
        }
        Location location = cityRep.getLocation();
        if (location != null) {
            vo.setLatitude(location.getLatitude().toString());
            vo.setLongitude(location.getLongitude().toString());
        }
        return vo;
    }

    /**
     * <pre>
     *     {
     *   "ret": 200,
     *   "msg": "success",
     *   "data": {
     *     "ip": "218.18.228.178",
     *     "long_ip": "3658671282",
     *     "isp": "中国电信",
     *     "area": "华南",
     *     "region_id": "440000",
     *     "region": "广东",
     *     "city_id": "440300",
     *     "city": "深圳",
     *     "district": "南山区",
     *     "district_id": "440305",
     *     "country_id": "CN",
     *     "country": "中国",
     *     "lat": "22.528499",
     *     "lng": "113.923552"
     *   },
     *   "log_id": "a00d44267348487d9555849cf6802835"
     * }
     * <pre/>
     * @param aliData
     *
     * @return
     */
    public static CommonRep from(String aliData) {
        if (aliData == null) {
            return null;
        }
        try {
            JSONObject json = JSON.parseObject(aliData);
            int ret = json.getIntValue("ret");
            if (ret != 200) {
                System.err.println("ali data error, code: " + ret + " msg:" + json.getString("msg"));
                return null;
            }
            JSONObject data = json.getJSONObject("data");
            CommonRep vo = new CommonRep();
            vo.setIp(data.getString("ip"));
            vo.setCountryCode(data.getString("country_id"));
            vo.setCountryName("");
            vo.setCountryZhName(data.getString("country"));

            vo.setSubdivisionCode("");
            vo.setSubdivisionName("");
            vo.setSubdivisionZhName(data.getString("region"));

            vo.setCityName("");
            vo.setCityZhName(data.getString("city"));

            vo.setLatitude(data.getString("lat"));
            vo.setLongitude(data.getString("lng"));
            return vo;
        } catch (Exception e) {
            System.err.println("转换失败:" + e);
            return null;
        }
    }

}
